import { Component } from '../core/Component';
import { Vector2 } from '../physics/Vector2';

export class Transform extends Component {
    private _position: Vector2;
    private _rotation: number;
    private _scale: Vector2;
    
    constructor(gameObject: Component['gameObject']) {
        super(gameObject);
        this._position = new Vector2(0, 0);
        this._rotation = 0;
        this._scale = new Vector2(1, 1);
    }
    
    public get position(): Vector2 {
        return this._position;
    }
    
    public set position(value: Vector2) {
        this._position = value;
    }
    
    public get rotation(): number {
        return this._rotation;
    }
    
    public set rotation(value: number) {
        // Normalize rotation to [0, 2π)
        this._rotation = ((value % (2 * Math.PI)) + 2 * Math.PI) % (2 * Math.PI);
    }
    
    public get scale(): Vector2 {
        return this._scale;
    }
    
    public set scale(value: Vector2) {
        this._scale = value;
    }
    
    /**
     * Get the world position of this transform
     */
    public get worldPosition(): Vector2 {
        if (!this.gameObject.parent) {
            return this._position;
        }
        
        const parentTransform = this.gameObject.parent.transform;
        const parentWorldPos = parentTransform.worldPosition;
        const parentRotation = parentTransform.rotation;
        const parentScale = parentTransform.scale;
        
        // Apply parent's transform
        const rotatedX = this._position.x * Math.cos(parentRotation) - this._position.y * Math.sin(parentRotation);
        const rotatedY = this._position.x * Math.sin(parentRotation) + this._position.y * Math.cos(parentRotation);
        
        return new Vector2(
            parentWorldPos.x + rotatedX * parentScale.x,
            parentWorldPos.y + rotatedY * parentScale.y
        );
    }
    
    /**
     * Get the world rotation of this transform
     */
    public get worldRotation(): number {
        if (!this.gameObject.parent) {
            return this._rotation;
        }
        
        return this.gameObject.parent.transform.worldRotation + this._rotation;
    }
    
    /**
     * Get the world scale of this transform
     */
    public get worldScale(): Vector2 {
        if (!this.gameObject.parent) {
            return this._scale;
        }
        
        const parentScale = this.gameObject.parent.transform.worldScale;
        return new Vector2(
            this._scale.x * parentScale.x,
            this._scale.y * parentScale.y
        );
    }
    
    /**
     * Translate the transform by the given vector
     */
    public translate(vector: Vector2): void {
        this._position = this._position.add(vector);
    }
    
    /**
     * Rotate the transform by the given angle in radians
     */
    public rotate(angle: number): void {
        this.rotation += angle;
    }
    
    /**
     * Scale the transform by the given vector
     */
    public scaleBy(vector: Vector2): void {
        this._scale = this._scale.multiply(vector);
    }
    
    /**
     * Look at a target position
     */
    public lookAt(target: Vector2): void {
        const direction = target.subtract(this._position);
        this.rotation = Math.atan2(direction.y, direction.x);
    }
    
    /**
     * Get the forward direction vector
     */
    public get forward(): Vector2 {
        return new Vector2(Math.cos(this._rotation), Math.sin(this._rotation));
    }
    
    /**
     * Get the right direction vector
     */
    public get right(): Vector2 {
        return new Vector2(Math.cos(this._rotation + Math.PI/2), Math.sin(this._rotation + Math.PI/2));
    }
} 